package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Override
    public void saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        Integer id = checkGroupDTO.getId();

        if (id!=null){
            //说明过来的是编辑请求  需要先删除中间表
            baseMapper.relieveByGroupId(id);
        }

        //添加检查组信息 维护中间表信息
        saveOrUpdate(checkGroupDTO);
        Integer gid = checkGroupDTO.getId();  //代码执行成功  checkGroupDTO拥有当前记录的主键

        Integer[]checkitemids = checkGroupDTO.getCheckitemids();
        System.out.println(checkitemids+"后台数组");
        for (Integer checkitemid : checkitemids) {
            baseMapper.addCheckGroupAndcheckitemids(gid,checkitemid);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Page<CheckGroup> page = null;
        PageResult result = null;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);  // 查询有效数据

        //      有条件分页查询  先判断条件是否为空
        if (StringUtils.isEmpty(pageBean.getQueryString())) {

            // mp  无分页查询     先配置 分页插件
            page = page(new Page<CheckGroup>(pageBean.getCurrentPage(), pageBean.getPageSize()), queryWrapper);

            //无条件查询结果
            result = new PageResult(page.getTotal(), page.getRecords());

            return result;
        } else {
            // 按照名称模糊查询
            queryWrapper.like("name", pageBean.getQueryString());

            //有条件查询结果
            page = page(new Page<CheckGroup>(pageBean.getCurrentPage(), pageBean.getPageSize()), queryWrapper);

            result = new PageResult(page.getTotal(), page.getRecords());
        }


        return result;
    }

    @Override
    public List<Integer> findCheckItemIdsByGroupId(int id) {
        //使用传递过来的检查组id  查询所有检查项
        return baseMapper.findCheckItemIdsByGroupId(id);
    }

    @Override
    public String deleteyGroupById(Integer id) {
        //先删除中间表 在删除Group
        try {
            int checkGroup = baseMapper.findCheckGroup(id);
            if (checkGroup==0){
            baseMapper.relieveByGroupId(id);
            baseMapper.deleteGroup(id);
            }else {
                return "该项目被套餐使用 无法删除";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);  // 查询有效数据
        List<CheckGroup> list = list(queryWrapper);
        return list;
    }


}

